package com.voice.server.command.impl;

import com.voice.core.message.AbstractMessage;
import com.voice.server.command.MessageCommand;
import com.voice.server.connection.ConnectionInvoke;
import com.voice.server.entity.AppToken;
import com.voice.server.observer.impl.ApiObserver;
import com.wangjiacai.utils.web.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component
public abstract class AbstractMessageCommand<T extends AbstractMessage> implements MessageCommand<T> {

    @Autowired
    private ConnectionInvoke connectionInvoke;
    @Autowired
    private HttpCycleExecute httpCycleExecute;
    @Autowired
    private RedisService redisService;

    @Override
    public void executeSdk(String userId, String deviceKey, T t) {
        connectionInvoke.sendSdkMessage(t.getUserId(),t.getTopic(),t);
    }

    @Override
    public void executeApi(String userId, String deviceKey, T t) {
        CompletableFuture.supplyAsync(()->redisService.hashGet(ApiObserver.hashKey,userId, AppToken.class))
                .thenAccept(appToken -> {
                    httpCycleExecute.execute(6,appToken.getPushUrl(),t.getTopic(),t.toString(),t.getSignString(appToken.getAppKey(),appToken.getAppSecret()),appToken.getAppKey());
                });
    }
}
